<?php
namespace Home\Controller;
use Think\Controller;
class ListController extends Controller{
	public function index(){
		//判断是否已经登录
		header("Content-type: text/html; charset=utf-8");
		if(!session('uid')){
			$this->error("请先登录！",U("Index/index"));
		}
		$username=session('username');
		
		$this->assign('username',$username);
		if(!$userList=M('userlist')){
			echo "error".$userList->getError();
		}
		//查看是否已经登记个人信息到系统
		$where['username']=$username;
		$info=$userList->where($where)->find();
		if(!$info||$info==null){
			$this->assign("notice","<strong>亲！</strong>您尚未完善<a href='".__APP__."/Home/List/editUserInfo'>个人信息</a>哦！");
			$uid=session('uid');
			$user=getUser($uid);
			$groupid=$user['groupid'];
			$grouptitle=getUserGroupTitle($groupid);
			
			//echo $grouptitle;
			$data['username']=$username;
			$data['bbs_group']=$grouptitle;
			$data['hmt_uid']=$uid;
			if($grouptitle=="灵魂人物"||$grouptitle=="退休人员"){
				$data['flag']=0;
			}else $data['flag']=1;
			$userList->add($data);
		}
		//查看是否已经完善信息
		
		
		//显示所有信息
		$count      = $userList->where()->count();// 查询管理员级别的总记录数
		$Page       = new \Think\Page($count,10); // 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show=$Page->show();// 分页显示输出
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		
		if(IS_GET){
			if(I('type')=="showFlagOnly"){
				$type['flag']=1;
			}
		}
		$list = $userList->join('hmt_group ON userlist.group_id=hmt_group.id')->where($type)->order('serial,grade')->limit($Page->firstRow.','.$Page->listRows)->select();
		
		//统计功能 
		$allMember=$userList->count();
		$flagMember=$userList->where('flag=1')->count();
		$groupList=M('hmt_group');
		$groupname=$groupList->order('serial')->getField("id,groupname");//获取分组
		//dump($groupname);
		$sortNum=array();
		foreach($groupname as $key=>$val){
			//$where['group_id']=$key;
			$num=$userList->where("group_id=$key")->count();
			$sortNum[$key]=$num;//分组ID＝》分组名
		}
		
		
		$this->assign('allMember',$allMember);
		$this->assign('flagMember',$flagMember);
		
		$this->assign("sortNum",$sortNum);
		$this->assign("groupTitle",$groupname);
		
		$this->assign('list',$list);// 赋值数据集
		$this->assign('page',$show);
		$this->assign("title","红满堂通讯录");
		$this->display();
	}
	public function logout(){
		session(null);
		$this->success("退出登录成功",U('Index/Index'));
	}
	
	public function userInfo(){
		$username="";
		if(I(username)){
			if(!session('username'))$this->error("您尚未登录");
			$username=I('username');
		}else{
			$username=session('username');
			if(!username||$username==""){
				$this->error('登录信息错误，尚未登录');
			}
		}
		$where['username']=$username;
		$userList=M("userlist");
		$user=$userList->join('hmt_group ON userlist.group_id=hmt_group.id')->where($where)->find();
		$isMark=null;
		if(!$user||$user==null){
			$isMark="(未登记)";
		}
		$user['flag']==1?$userflag="在任":$userflag="不在任";
		$this->assign('userflag',$userflag);
		$this->assign('isMark',$isMark);
		$this->assign("user",$user);
		$this->assign('username',$username);
		//dump($user);
		$this->display();
	}
	
	public function editUserInfo(){
		//编辑个人信息
		$username=session('username');
		if(!username||$username==""){
			$this->error('登录信息错误，尚未登录');
		}
		$where['username']=$username;
		$userList=M("userlist");
		
		if(IS_POST){
			//保存表单数据，并返回结果
			$userList->where($where)->save($_POST);
			$this->ajaxReturn("更新成功");
		}
		//dump(F('user'));
		
		$user=$userList->join('hmt_group ON userlist.group_id=hmt_group.id')->where($where)->find();
		$groupselect=M('hmt_group')->select();
		$this->assign('groupselect',$groupselect);
		$this->assign('sex',$user.sex);
		$user['flag']==1?$userflag="在任":$userflag="不在任";
		$this->assign('userflag',$userflag);
		$this->assign("user",$user);
		//dump($user);
		$this->display();
	}

	public function sortInfo(){
		if(IS_GET){
			$groupname= I('groupname');
			$groupList=M('hmt_group');
			$hmt_group=$groupList->order('serial')->getField("id,groupname");//获取分组
				
			$group_id=array_search($groupname, $hmt_group);
			//echo $group_id;
			$userList=M('userlist');
			$where['group_id']=$group_id;
			$user=$userList->where($where)->select();
			dump($user);
			$this->assign('groupname',$groupname);
			$this->assign('user',$user);
			$this->display();
		}else $this->error("参数错误");
	}
	/**
	 * 同步信息，主要为了同步用户组，如果某人卸任后，红满堂用户组发生变化，则可以通过同步来更新系统用户组
	 * Enter description here ...
	 */
	public function synUpdate(){
		$uid=session('uid');
		if(!$uid)$this->error("您尚未登录");
	
		echo "访问论坛中……<br>更新用户组……<br>更新在任状态……";
		$user=getUser($uid);
		$groupid=$user['groupid'];
		$grouptitle=getUserGroupTitle($groupid);
		if($grouptitle=="灵魂人物"||$grouptitle=="退休人员")$data['flag']=0;
		$data['bbs_group']=$grouptitle;
		$userList=M('userlist')->where("hmt_uid=$uid")->save($data);
		
		$this->success("更新成功！");
	}
	public function help(){
		$this->display();
	}
}